//
//  LauchViewController.m
//  Traveling
//
//  Created by leihuiwu on 2019/3/5.
//  Copyright © 2019 zhengwenqing’s mac. All rights reserved.
//

#import "LauchViewController.h"

#import "WebViewController.h"

@interface LauchViewController ()

@property (nonatomic, weak) IBOutlet UIButton *refreshTryBtn;

@property (nonatomic, assign) BOOL isHasNoNetwork;

@end



@implementation LauchViewController

- (void)dealloc {
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self listenNetWorkingStatus];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *show_icon = [userDefaults objectForKey:@"show_icon"];
    NSString *iconimage = [userDefaults objectForKey:@"iconimage"];
    
    if ([show_icon length] > 0 && [iconimage length] > 0) {
        [self requestAppInfoInShowLoading:NO];
        [self gotoWebVC];
    }else {
        [self requestAppInfoInShowLoading:YES];
    }

}



-(void)listenNetWorkingStatus{
    
    //1:创建网络监听者
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    //开启网络监听
    [manager startMonitoring];
    //2:获取网络状态
    /*
     AFNetworkReachabilityStatusUnknown          = 未知网络，
     AFNetworkReachabilityStatusNotReachable     = 没有联网
     AFNetworkReachabilityStatusReachableViaWWAN = 蜂窝数据
     AFNetworkReachabilityStatusReachableViaWiFi = 无线网
     */
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"未知网络");
                self.isHasNoNetwork = YES;
                break;
            case AFNetworkReachabilityStatusNotReachable:
                self.isHasNoNetwork = YES;
                NSLog(@"没有联网");
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"蜂窝数据");
                if (self.isHasNoNetwork) {
                    [self requestAppInfoInShowLoading:YES];
                    self.isHasNoNetwork = NO;
                }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                if (self.isHasNoNetwork) {
                    [self requestAppInfoInShowLoading:YES];
                    self.isHasNoNetwork = NO;
                }
                NSLog(@"无线网");
                break;
            default:
                break;
        }
    }];
    

}

- (void)gotoWebVC {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *iconimage = [userDefaults objectForKey:@"iconimage"];
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    WebViewController *webVC = [mainStoryBoard instantiateViewControllerWithIdentifier:@"WebViewController"];
    webVC.hidesBottomBarWhenPushed = YES;
    webVC.backURLStr = iconimage;
    webVC.isMaJiaBool = YES;
    [[UIApplication sharedApplication].delegate  window].rootViewController = webVC;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)requestAppInfoInShowLoading:(BOOL)isShowLoading {
    
    MBProgressHUD *hud = nil;
    if (isShowLoading) {
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"正在加载...";
        hud.opacity = 0.5f;
        hud.labelColor = [UIColor whiteColor];
        hud.activityIndicatorColor = [UIColor blackColor];
        hud.labelFont = [UIFont systemFontOfSize:10];
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [DataManager getAppInfo:@"123456789" CompletionHandler:^(NSDictionary *response, NSError *error) {
        
        if (isShowLoading) {
            [hud hide:YES afterDelay:2.f];
        }
        
        NSString *code = response[@"code"];
        if ([code integerValue] == 200) { // 成功
            NSDictionary *result = response[@"result"];
            NSString *show_icon = result[@"show_icon"];
            NSString *iconimage = result[@"iconimage"];
            if ([show_icon integerValue] == 1) { // 显示webView
                [userDefaults setObject:show_icon forKey:@"show_icon"];
                [userDefaults setObject:iconimage forKey:@"iconimage"];
                [userDefaults synchronize];
                [self gotoWebVC];
            }
        }else if (error) {
            if (isShowLoading) {
                self.refreshTryBtn.hidden =  NO;
            }
        }
    }];
    
}

- (IBAction)refreshTryBtnAction:(id)sender {
    self.refreshTryBtn.hidden = YES;
    
    [self requestAppInfoInShowLoading:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
